package fr.aprr.geoloc.client.presenser;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;

import fr.aprr.geoloc.client.AppEventBus;
import fr.aprr.geoloc.client.presenser.interfaces.IVehiculeView;
import fr.aprr.geoloc.client.service.OdysseeServiceAsync;
import fr.aprr.geoloc.client.view.VehiculeView;
import fr.aprr.geoloc.shared.domain.Odyssee;

@Presenter(view = VehiculeView.class)
public class VehiculePresenter extends BasePresenter<IVehiculeView, AppEventBus>
{
	@Inject
	private OdysseeServiceAsync odysseeService = null;
	
	@Override
	public void bind() {
		// TODO Auto-generated method stub
		view.getButton().addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
			//	eventBus.textChange(view.getTextField().getText());*
				getOdyssee(Integer.parseInt(view.getTextField().getText()));
			}
		});
	}
	
	public void getOdyssee(Integer id) {
		AsyncCallback<Odyssee> callback = new AsyncCallback<Odyssee>() {

			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				System.out.println("Eurrer : "+ caught.getMessage());
			}

			public void onSuccess(Odyssee result) {
				// TODO Auto-generated method stub
				eventBus.textChange(result.getNomChamp());
			}
			
		};
		
		odysseeService.getOdyssee(id, callback);
	}

	public void onTextChange(String input){
		view.getLabel().setText(input);
	}
	
	public void onStart(){
		eventBus.add2Tab(view);
	};
}
